package javaWeb;

import Bean.*;
import Service.CartService;
import Service.FurnService;
import Service.Impl.CartServiceImpl;
import Service.Impl.FurnServiceImpl;
import Utils.Tool;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CartServlet extends BasicServlet {
    HashMap<Integer, Cart<Furn>> hashMap = new HashMap<>();
    private FurnService furnService = new FurnServiceImpl();
    private CartService cartService = new CartServiceImpl();

    protected void addFurn(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String furnId = req.getParameter("FurnId");
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("User");
        if (!cartService.isCartFurn(user.getId(), Tool.getNumber(furnId,1), CartServlet.class)){
            cartService.addCartFurn(user.getId(),Tool.getNumber(furnId,1));
        }
        req.getRequestDispatcher("/javaWeb/CartServlet?action=showCart").forward(req,resp);
//        HttpSession session = req.getSession();
//        User user = (User) session.getAttribute("User");
//        if (!hashMap.containsKey(user.getId())){
//            hashMap.put(user.getId(), new Cart<>());
//        }
//        //初始化,//这个是加表，下一个是
//        Cart<Furn> furnCart = hashMap.get(user.getId());
//        furnCart.setKeyWord(keyWord);
//        furnCart.setBeginIndex(Tool.getNumber(pageNo,-1));
//        String sql ="select id, FurnName, Merchant, Price, Volume, inventory, img_path img  from Furn where id =?";
//        furnCart.addFurn(furnService.getFurn(sql,Tool.getNumber(furnId,1)));
//        furnCart.setFurnGood(Tool.getNumber(furnId,1),1);
    }

    protected void showCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageNo = req.getParameter("PageNo");
        String keyWord = req.getParameter("keyWord");
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("User");
        if (!(hashMap.containsKey(user.getId()))){
            hashMap.put(user.getId(), new Cart<>());
        }
        Cart<Furn> furnCart = hashMap.get(user.getId());
        furnCart.setKeyWord(keyWord);

        if (!(pageNo == null && furnCart.getBeginIndex() != 1)){
            furnCart.setBeginIndex(Tool.getNumber(pageNo,1));
        }
        furnCart.setCarrGoods(cartService.getAllCartFurn(user.getId()));
        furnCart.setGoods(furnService.setGoods(furnCart.getCartGoods()));
        session.setAttribute("FurnCart",furnCart);
        hashMap.put(user.getId(), furnCart);
        resp.sendRedirect("/Web/Cart/cart.jsp");
    }
    protected void deleteFurn(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String furnId = req.getParameter("FurnID");
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("User");
        cartService.deleteCartFurn(user.getId(),Tool.getNumber(furnId,1));
        req.getRequestDispatcher("/javaWeb/CartServlet?action=showCart").forward(req,resp);
    }
    protected void ClearCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("User");
        cartService.deleteCartFurn(user.getId());
        req.getRequestDispatcher("/javaWeb/CartServlet?action=showCart").forward(req,resp);
    }
    protected void clearOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("User");
        cartService.deleteCartFurn(user.getId());
        String number = (String) req.getAttribute("OrderNumber");
        session.setAttribute("OrderNumber",number);
        resp.sendRedirect("/Web/Order/checkout.jsp");
    }


    protected void Continue(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String header = req.getHeader("Referer");
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("User");
        Cart<Furn> furnCart = hashMap.get(user.getId());
        //resp.sendRedirect(header);
        req.getRequestDispatcher("/javaWeb/CustomerFurnPage?action=FurnPage"+"&PageNo="+furnCart.getBeginIndex()).forward(req,resp);
    }
    protected void getFurnNumber(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String furnId = req.getParameter("FurnId");
        String size = req.getParameter("Size");
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("User");
        cartService.SetCartSize(user.getId(), Tool.getNumber(furnId,1),Tool.getNumber(size,1));
        Cart<Furn> furnCart = hashMap.get(user.getId());


        BigDecimal bigDecimal = furnService.calculatePrice(user.getId());

        hashMap.put(user.getId(), furnCart);
        String number = req.getParameter("FurnNumber");
        resp.getWriter().write("共"+number+"床品"+" " +"总价为"+bigDecimal );

    }
    protected void getCartFurn(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("User");
        Integer integer = cartService.CartIdNumber(user.getId());
        resp.getWriter().write(String.valueOf(integer));
    }
    protected void endCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("User");
        // 先添加一个订单
        Order order = new Order();
        order.setDatetime(Tool.getDateTime());
        order.setOrderNumber(String.valueOf(Tool.getOrderNumber()));
        order.setSumPrice(furnService.calculatePrice(user.getId()));
        List<Cart.CartGood> cartGoods = cartService.getAllCartFurn(user.getId());
    }
    protected void OtherAddFurn(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String furnId = req.getParameter("FurnId");
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("User");
        if (!cartService.isCartFurn(user.getId(), Tool.getNumber(furnId,1), CartServlet.class)){
            cartService.addCartFurn(user.getId(),Tool.getNumber(furnId,1));
        }
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("Xing","ok");
        Gson gson = new Gson();
        String json = gson.toJson(hashMap);
        System.out.println("ok");
        resp.getWriter().write(json);
    }
    protected void getCartCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("User");
        Integer integer = cartService.CartIdNumber(user.getId());
        Gson gson = new Gson();
        HashMap<String, Integer> hashMap = new HashMap<>();
        hashMap.put("CartAmount",integer);
        Page<Furn> furnPage = (Page<Furn>) session.getAttribute("FurnPage");
        String json = gson.toJson(hashMap);
        furnPage.setCartAmount(integer);
        System.out.println("ok2");
        resp.getWriter().write(json);
    }




}
